package Q8;

import java.util.Scanner;

public class Question8 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取每个进程申请的内存块数
        String[] requests = scanner.nextLine().split(" ");
        int[] processRequests = new int[4];
        for (int i = 0; i < 4; i++) {
            processRequests[i] = Integer.parseInt(requests[i]);
        }

        // 读取进程编号和页号
        String[] input = scanner.nextLine().split(" ");
        int processId = Integer.parseInt(input[0]) - 1; // 进程编号转为下标
        int pageNumber = Integer.parseInt(input[1]);

        // 内存块的状态
        boolean[] memoryBlocks = new boolean[64];
        // 已占用的块
        int[] occupiedBlocks = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};

        // 标记已占用的块
        for (int block : occupiedBlocks) {
            memoryBlocks[block] = true;
        }

        // 计算可用内存块
        int totalRequest = processRequests[0] + processRequests[1] + processRequests[2] + processRequests[3];
        if (totalRequest > 54) {
            System.out.println("wait");
            return;
        }

        // 寻找空闲块
        int[][] allocatedBlocks = new int[4][54]; // 每个进程最多54块
        int[] blockCount = new int[4]; // 每个进程已分配的块数

        // 从左到右扫描内存块
        for (int i = 0; i < 64; i++) {
            if (!memoryBlocks[i]) { // 如果该块是空闲的
                // 分配块给对应的进程
                for (int j = 0; j < 4; j++) {
                    if (blockCount[j] < processRequests[j]) {
                        allocatedBlocks[j][blockCount[j]] = i; // 分配内存块
                        blockCount[j]++;
                        break;
                    }
                }
            }
        }

        // 检查进程N的请求
        if (processRequests[processId] > 54) {
            System.out.println("wait");
            return;
        }

        if (pageNumber < 0 || pageNumber >= processRequests[processId]) {
            System.out.println("error");
            return;
        }

        // 输出对应页面的内存块号
        System.out.println(allocatedBlocks[processId][pageNumber]);
    }
}
